package cn.itcast.www.jspservletTest;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.TagSupport;
//循环标签
public class GreetTag extends BodyTagSupport {
	private int count;

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}
	
	/**
	 * 只有当doStartTag()方法的返回值是EVAL_BODY_BUFFERED时，
	 * JSP页面的实例对象才会创建BodyContent对象，调用setBodyContent()和
	 * doInitBody()方法。
	 */
	public int doStartTag() throws JspException {
		count = 0;
		return EVAL_BODY_BUFFERED;
	}
	
	/**
	 * 因为在doAfterBody()方法被调用之前，标签体已经被执行过一次，
	 * 所以在这里虽然是两次循环，但实际上会输出三段标签体的内容。
	 */
	public int doAfterBody() throws JspException {
		if(count < 2){
			count++;
			return EVAL_BODY_AGAIN;
		}else{
			return SKIP_BODY;
		}
	}

	@Override
	public int doEndTag() throws JspException {
		JspWriter out = bodyContent.getEnclosingWriter();
		try {
			out.println(bodyContent.getString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}
}
